package com.damai.utils.value;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;

import com.citywithincity.db.DBException;
import com.citywithincity.db.interfaces.IValueConvert;

class Clob2String implements IValueConvert {
	Clob2String() {

	}

	@Override
	public Object convert(Object value) {
		Clob clob = (Clob) value;
		if (clob == null)
			return null;
		BufferedReader bufferedReader = null;
		try {
			Reader reader = clob.getCharacterStream();
			bufferedReader = new BufferedReader(reader);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				sb.append(line);
			}
			return sb.toString();
		} catch (SQLException e) {
			throw new DBException(e);
		} catch (IOException e) {
			throw new DBException(e);
		} finally {
			try {
				bufferedReader.close();
			} catch (IOException e) {
			}
		}
	}

}
